using System;

using WP.MF.DataAccess;
using WP.MF.DataEntity;
using WP.MF.BusinessRule;
using WP.DB;
using WP.Commons;
using System.Data ; 

namespace WP.MF.BusinessLogical
{
	/// <summary>
	/// Summary description for MUserBiz.
	/// </summary>
    public class MUserBLL : IBusinessLogical
	{
        MUserDAL mUserDAL = new MUserDAL();

		public MUserBLL()
		{
			//
			// TODO: Add constructor logic here
			//
		}

        #region ***For implement interface

        #region Select Record by RECID OverLoad +1
        public MUserDEL SelectDataByRecID(DBAccess dBAccess, string strRecId)
        {
            MUserDEL mUserDEL = new MUserDEL();

            DataFunctions.SelectByRecID(dBAccess, mUserDEL, mUserDAL.Fields, strRecId);

            return mUserDEL;
        }

        public IDataEntity SelectDataByRecID(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, false);
            try
            {
                MUserDEL mUserDEL = SelectDataByRecID(dBAccess, strRecId);
                return mUserDEL as IDataEntity;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

        #region Insert MUser
        public bool Insert(DBAccess dBAccess, MUserDEL mUserDEL)
        {
            bool bFlag = true;
            MUserBRL mUserBRL = new MUserBRL();

            //BaseFunctions.SpecialCharDeal(mUserDEL);

            if (mUserBRL.InsertValid(dBAccess, mUserDEL))
            {
                mUserDAL.InsertMUser(dBAccess, mUserDEL);

            }
            else
            {
                bFlag = false;
            }
            return bFlag;

        }

        public bool Insert(IDataEntity iMUserDEL)
        {
            bool bReturn = true;
            DBAccess dBAccess = null;
            MUserDEL mUserDEL = iMUserDEL as MUserDEL;
            dBAccess = new DBAccess(false, true);

            try
            {
                bReturn = Insert(dBAccess, mUserDEL);
                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                    ReflectMethod.frmMainMenu_RefreshMenu();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTran();
                bReturn = false;
                ExceptionManager.ProcessException(e);
            }

            return bReturn;
        }
        #endregion

        #region Update MUser
        public bool Update(DBAccess dBAccess, MUserDEL mUserDEL)
        {
            bool bFlag = true;
            MUserBRL mUserBRL = new MUserBRL();

            //BaseFunctions.SpecialCharDeal(mUserDEL);

            if (mUserBRL.UpdateValid(dBAccess, mUserDEL))
            {
                mUserDAL.UpdateMUser(dBAccess, mUserDEL);

            }
            else
            {
                bFlag = false;
            }
            return bFlag;
        }

        public bool Update(IDataEntity iMUserDEL)
        {
            bool bReturn = true;
            MUserDEL mUserDEL = iMUserDEL as MUserDEL;
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                bReturn = Update(dBAccess, mUserDEL);
                if (bReturn)
                {
                    dBAccess.CommitTranAndCloseCon();
                    ReflectMethod.frmMainMenu_RefreshMenu();
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTran();
                ExceptionManager.ProcessException(e);
                bReturn = false;
            }

            return bReturn;
        }
        #endregion

        #region Delete MUser
        public bool Delete(string strRecId, byte[] btOldRecStamp)
        {
            MUserBRL mUserBRL = new MUserBRL();
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                if (mUserBRL.DeleteValid(dBAccess, strRecId, btOldRecStamp))
                {
                    mUserDAL.DeleteMUser(dBAccess, int.Parse(strRecId));
                    dBAccess.CommitTranAndCloseCon();
                    ReflectMethod.frmMainMenu_RefreshMenu();
                    return true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                    return false;
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

        #endregion

        #endregion***

        #region Copy MUser
        public bool CopyMUser(string strOldName, string strNewName, string strNewDesc)
        {
            MUserBRL mUserBRL = new MUserBRL();
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                if (mUserBRL.CopyValid(dBAccess, strNewName, strNewDesc))
                {
                    mUserDAL.CopyMUser(dBAccess, strOldName, strNewName, strNewDesc);
                    dBAccess.CommitTranAndCloseCon();
                    ReflectMethod.frmMainMenu_RefreshMenu();
                    return true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                    return false;
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }
        #endregion

        public bool Login()
        {
            string strSql = "";
            string strUserName = "";
            string strPassword = "";

            MUserDAL mUserDAL = new MUserDAL();
            MUserDEL mUserDEL = new MUserDEL();
            DBAccess dBAccess = new DBAccess();
            strUserName = SysVar.UserName;
            strPassword = SysVar.Password;

            strSql = mUserDAL.SelectByUserName(strUserName);
            dBAccess.GetDataSet(strSql, MUserDEL.TABLENAME_MUSER, mUserDEL);

            if (mUserDEL.Tables[MUserDEL.TABLENAME_MUSER].Rows.Count == 1)
            {
                if (mUserDEL.Tables[MUserDEL.TABLENAME_MUSER].Rows[0][MUserDEL.PASSWORD].ToString() == strPassword)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public MUserDEL SelectByUserName(string strUserName)
        {
            string strSql = "";
            MUserDAL mUserDAL = new MUserDAL();
            MUserDEL mUserDEL = null;
            DBAccess dBAccess = new DBAccess();

            strSql = mUserDAL.SelectByUserName(strUserName);
            dBAccess.GetDataSet(strSql, MUserDEL.TABLENAME_MUSER, mUserDEL);

            return mUserDEL;
        }

        public string SelectOneByUserName(string strUserName)
        {
            string strSql = "";
            MUserDAL mUserDAL = new MUserDAL();
            DBAccess dBAccess = new DBAccess();

            strSql = mUserDAL.SelectOneByUserName(strUserName);

            return dBAccess.GetValue(strSql);
        }

        public string SelectOneByUserName(string strUserName, string fieldName)
        {
            string strSql = "";
            MUserDAL mUserDAL = new MUserDAL();
            DBAccess dBAccess = new DBAccess();

            strSql = mUserDAL.SelectOneByUserName(strUserName, fieldName);

            return dBAccess.GetValue(strSql);

        }


        #region  mUserAlert


        public bool InsertmUserAlert(DataRow dr)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {

                mUserDAL.InsertmUserAlert(dBAccess, dr);
                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

        public bool UpdatemUserAlert(DataRow dr)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                mUserDAL.UpdatemUserAlert(dBAccess, dr);

                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }


        public bool DeletemUserAlert(int iRecID)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                    mUserDAL.DeletemUserAlert(dBAccess, iRecID);
                    dBAccess.CommitTranAndCloseCon();

                    return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

        #endregion

    }
}
